$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var material = "";
    var pass = 1;

    //搜索框 获取焦点
    $("#goods_id").focus();
    $(".content").on("click", function () {
        $("#goods_id").val("").focus();
    });

    //点击 返回 箭头
    $(".head .goBack").on("click", function () {
        window.location.href = "heatfeed.html";
    });

    //根据扫描内容 得到 原材料
    $(document).on("keydown", "#goods_id", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描热处理原料！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            $(".content .list-block ul .infinite-scroll-preloader").show();
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/heat/getHeatMaterial',
                data: {
                    id: localStorage.getItem("work_id"),
                    machine: localStorage.getItem("heatMachine"),
                    number: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    //加载提示符 隐藏
                    $(".content .list-block ul .infinite-scroll-preloader").hide();
                    if (res.success) {
                        var data = res.result;
                        //console.log(data);
                        $alert(res.message);
                        material = data;
                        //填充 页面
                        $(".section span.number").html(data.batch);
                        $(".section span.gggh").html(data.inventorySpecification);
                        $(".section span.lh").html(data.stove);
                        $(".section span.bh").html(data.column);
                        $(".section span.zl").html(data.availableQuantity);
                        $(".section span.pcdh").html(data.workplanNumber);
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    $(".content .list-block ul .infinite-scroll-preloader").hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 取消 按钮
    $(".foot .quxiao").on("click", function () {
        window.location.href = "heatfeed.html";
    });

    //点击 确定 按钮
    $(".foot .queding").on("click", function () {
        if (!material) {
            $alert("请扫描热处理原料！");
            setTimeout(function () {
                $("#goods_id").val("").focus();
            }, 1400);
        }
        else {
            if (material.workplanNumber && material.plan.workplanNumber.indexOf(material.workplanNumber) == -1) {
                $.modal({
                    title: '排产单不一致，是否上料？',
                    buttons: [
                        {
                            text: '否',
                            onClick: function () {

                            }
                        }, {
                            text: '是',
                            onClick: function () {
                                if (material.alias == "silk" && material.quality && material.quality == "不合格") {
                                    $.confirm("拉丝产品不合格，确定上料？",function () {
                                        if (pass == 0) {
                                            return
                                        }
                                        pass = 0;
                                        loading_show("上料中");
                                        $.ajax({
                                            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                                            type: "post",
                                            url: url + '/app/heat/feedHeatMachine',
                                            data: {
                                                planId: material.plan._id,
                                                materialType: material.materialType,
                                                id: localStorage.getItem("work_id"),
                                                machine: localStorage.getItem("heatMachine"),
                                                number: $(".section span.number").text()
                                            },
                                            dataType: 'json',
                                            json: 'callback',
                                            success: function (res) {
                                                pass = 1;
                                                if (res.success) {
                                                    loading_hide();
                                                    //上料 成功 跳转 页面
                                                    window.location.href = "heatfeed.html";
                                                } else {
                                                    loading_hide();
                                                    setTimeout(function () {
                                                        $("#goods_id").val("").focus();
                                                    }, 1400);
                                                    $alert(res.message);
                                                }
                                            },
                                            error: function (error) {
                                                pass = 1;
                                                loading_hide();
                                                console.log(error);
                                                setTimeout(function () {
                                                    $("#goods_id").val("").focus();
                                                }, 1400);
                                                $alert(error.responseText);
                                            }
                                        });
                                    })
                                } else {
                                    if (pass == 0) {
                                        return
                                    }
                                    pass = 0;
                                    loading_show("上料中");
                                    $.ajax({
                                        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                                        type: "post",
                                        url: url + '/app/heat/feedHeatMachine',
                                        data: {
                                            planId: material.plan._id,
                                            materialType: material.materialType,
                                            id: localStorage.getItem("work_id"),
                                            machine: localStorage.getItem("heatMachine"),
                                            number: $(".section span.number").text()
                                        },
                                        dataType: 'json',
                                        json: 'callback',
                                        success: function (res) {
                                            pass = 1;
                                            if (res.success) {
                                                loading_hide();
                                                //上料 成功 跳转 页面
                                                window.location.href = "heatfeed.html";
                                            } else {
                                                loading_hide();
                                                setTimeout(function () {
                                                    $("#goods_id").val("").focus();
                                                }, 1400);
                                                $alert(res.message);
                                            }
                                        },
                                        error: function (error) {
                                            pass = 1;
                                            loading_hide();
                                            console.log(error);
                                            setTimeout(function () {
                                                $("#goods_id").val("").focus();
                                            }, 1400);
                                            $alert(error.responseText);
                                        }
                                    });
                                }
                            }
                        }
                    ]
                });
            } else {
                if (material.alias == "silkwarehouse" && material.quality && material.quality == "不合格") {
                    $.confirm("拉丝产品不合格，确定上料？",function () {
                        if (pass == 0) {
                            return
                        }
                        pass = 0;
                        loading_show("上料中");
                        $.ajax({
                            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                            type: "post",
                            url: url + '/app/heat/feedHeatMachine',
                            data: {
                                planId: material.plan._id,
                                materialType: material.materialType,
                                id: localStorage.getItem("work_id"),
                                machine: localStorage.getItem("heatMachine"),
                                number: $(".section span.number").text()
                            },
                            dataType: 'json',
                            json: 'callback',
                            success: function (res) {
                                pass = 1;
                                if (res.success) {
                                    loading_hide();
                                    //上料 成功 跳转 页面
                                    window.location.href = "heatfeed.html";
                                } else {
                                    loading_hide();
                                    setTimeout(function () {
                                        $("#goods_id").val("").focus();
                                    }, 1400);
                                    $alert(res.message);
                                }
                            },
                            error: function (error) {
                                pass = 1;
                                loading_hide();
                                console.log(error);
                                setTimeout(function () {
                                    $("#goods_id").val("").focus();
                                }, 1400);
                                $alert(error.responseText);
                            }
                        });
                    })
                } else {
                    if (pass == 0) {
                        return
                    }
                    pass = 0;
                    loading_show("上料中");
                    $.ajax({
                        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                        type: "post",
                        url: url + '/app/heat/feedHeatMachine',
                        data: {
                            planId: material.plan._id,
                            materialType: material.materialType,
                            id: localStorage.getItem("work_id"),
                            machine: localStorage.getItem("heatMachine"),
                            number: $(".section span.number").text()
                        },
                        dataType: 'json',
                        json: 'callback',
                        success: function (res) {
                            pass = 1;
                            if (res.success) {
                                loading_hide();
                                //上料 成功 跳转 页面
                                window.location.href = "heatfeed.html";
                            } else {
                                loading_hide();
                                setTimeout(function () {
                                    $("#goods_id").val("").focus();
                                }, 1400);
                                $alert(res.message);
                            }
                        },
                        error: function (error) {
                            pass = 1;
                            loading_hide();
                            console.log(error);
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                            $alert(error.responseText);
                        }
                    });
                }
            }
        }
    });

});
